Completed
Pull Request — master (#171)
by Johan
34s
created

applyOptions.js ➔ convertOption   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 14
rs 9.2
1
export default applyOptions;
2
3
function applyOptions(optionsBlueprint, options) {
4
	var returnOptions = {};
5
	for (var name in optionsBlueprint) {
6
		if (optionsBlueprint.hasOwnProperty(name)) {
7
			if (typeof options[name] !== 'undefined') {
8
				returnOptions[name] = convertOption(options[name], optionsBlueprint[name].type);
9
			}
10
			else {
11
				returnOptions[name] = optionsBlueprint[name].default;
12
			}
13
		}
14
	}
15
	return returnOptions;
16
}
17
18
// Convert an option to a specified type
19
function convertOption(data, type) {
20
	if (type === 'string') {
21
		return '' + data;
22
	}
23
	else if (type === 'number') {
24
		return parseInt(data, 10);
25
	}
26
	else if (type === 'boolean') {
27
		return (typeof data === 'boolean' && data) || data === 'true' || data === 'True';
28
	}
29
	else {
30
		return data;
31
	}
32
}
33